package com.efast.dmom.mes.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.Param;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.ParamHandler;
import com.efast.dmom.mes.service.ParamService;

@Service
public class ParamServiceImpl implements ParamService {

	@Autowired
	private ParamHandler paramHandler;
	
	@Override
	public ResultBean save(Param param) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if(param==null || param.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		String name = param.getInnerData().getName();
		long paramGroupId = param.getInnerData().getParamGroupId();
		if("".equals(name)) {
			resultBean.setSuccess(false);
			resultBean.setMessage("参数名称为必填项");
			return resultBean;
		}
		if(paramGroupId==0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法获取参数组主键id");
			return resultBean;
		}
		Param param2 = paramHandler.getByNameAndParamGroupId(paramGroupId, name);
		if(param2.getInnerData() !=null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("参数名称已经存在");
			return resultBean;
		}
		paramHandler.save(param);
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功");
		return resultBean;
	}

	@Override
	public ResultBean update(Param param) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if(param==null || param.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		String name = param.getInnerData().getName();
		long paramGroupId = param.getInnerData().getParamGroupId();
		if("".equals(name)) {
			resultBean.setSuccess(false);
			resultBean.setMessage("参数名称为必填项");
			return resultBean;
		}
		if(paramGroupId==0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法获取参数组主键id");
			return resultBean;
		}
		Param param2 = paramHandler.getById(param.getInnerData().getId());
		if(param2.getInnerData() ==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!！");
			return resultBean;
		}
		if(!param2.getInnerData().getName().equals(name)) {
			resultBean.setSuccess(false);
			resultBean.setMessage("参数名不可编辑！");
			return resultBean;
		}
		paramHandler.update(param);
		resultBean.setSuccess(true);
		resultBean.setMessage("修改成功");
		return resultBean;
	}

	@Override
	public ResultBean delete(Param param) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if(param==null || param.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		paramHandler.delete(param);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功");
		return resultBean;
	}

}
